package com.ebelter.bodyfatscale.ui.activity;


import android.content.Intent;
import android.util.TypedValue;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.adapter.HostoryListAdapter;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.HostoryListData;
import com.ebelter.bodyfatscale.moudules.https.response.HttpsParameters;
import com.ebelter.bodyfatscale.util.StringUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.bodyfatscale.util.ViewUtils;
import com.ebelter.btlibrary.util.ULog;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class HistoryListActivity extends BaseActivity {

    private static final String TAG = "HistoryListActivity";


    public static final int REQUEST_CODE = 171;


    @BindView(R.id.modify_back_bt)
    ImageView back_iv;


    @BindView(R.id.no_history_ly)
    RelativeLayout no_history_ly;

    @BindView(R.id.history_lv)
    ListView history_lv;

    private HostoryListAdapter adapter;


    private int startPage;
    private int pageSize=10;

    private long lastLoadingTime;

    @Override
    protected int loadLayoutById() {
        return R.layout.historylist_ly;
    }

    @Override
    protected void initViews() {
        startPage=0;
        pageSize=10;
      //  StatusBarUtil.setDarkStatusIcon(this,false);
        history_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ULog.i(TAG,"----获取到的数据是---"+adapter.getData().get(position));
                Intent deatilIntent=new Intent(HistoryListActivity.this,HistoryItemDetailActivity.class);
                deatilIntent.putExtra(HistoryItemDetailActivity.ITEMDATD,adapter.getData().get(position));
                startActivity(deatilIntent);
            }
        });
    }
    View listFootView;
    TextView noMoreTv;
    @Override
    protected void initData() {

        adapter=new HostoryListAdapter(this);
        history_lv.setAdapter(adapter);

        listFootView = View.inflate(this, R.layout.no_history, null);
        noMoreTv=listFootView.findViewById(R.id.no_data_tv);
        noMoreTv.setText(R.string.No_more);
        noMoreTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        history_lv.addFooterView(listFootView);
        ViewUtils.hideView(listFootView);

        getHostoryListDatas(startPage,pageSize);
        history_lv.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (System.currentTimeMillis()-lastLoadingTime>3000){
                            ULog.i(TAG,"---------加载更多");
                            lastLoadingTime=System.currentTimeMillis();
                            getHostoryListDatas(++startPage,pageSize);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

    }


    @OnClick({ R.id.modify_back_bt})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modify_back_bt:
                finish();
                break;
        }
    }



    public void getHostoryListDatas(int startPage,int pageSize) {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        showLoadingDialog(StringUtils.getResStr(R.string.loading));
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(HttpsParameters.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(HttpsParameters.START_INDEX, startPage);
        paramMap.put(HttpsParameters.PAGE_SIZE, pageSize);
        NetUtils.getInstance().getsHostoryListData(this, paramMap, new HttpResponse<HostoryListData>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, HostoryListData hostoryListData) {


                dissLoadingDialog();
                if (isRequestSuccess){
                    ULog.i(TAG,requestDes);
                    ULog.i(TAG,hostoryListData.toString());
                    if (hostoryListData.resultCode==0){
                        //2018-06-19 12:15:12
                        if (adapter!=null){
                            if (hostoryListData.getResultData()!=null&&hostoryListData.getResultData().getData()!=null){
                                adapter.addData(hostoryListData.getResultData().getData());
                                if (hostoryListData.getResultData().getData().size()==0){
                                    ViewUtils.displayView(listFootView);
                                }
                            }

                        }

                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }

                if (adapter!=null){
                    if (adapter.getCount()==0){
                        ViewUtils.displayView(no_history_ly);
                    }else {
                        ViewUtils.hideView(no_history_ly);
                    }
                }


            }
        });
    }
}
